package json.org.noahx.javavsjson;

import java.io.*;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: noah
 * Date: 3/8/13
 * Time: 10:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class JavaSerializationTest implements SerializationTest {

    @Override
    public String getTestName() {
        return "Java";
    }

    @Override
    public Map<String, Object> testBytes2Map(byte[] bytes) {
        Map<String, Object> result = null;
        try {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ObjectInputStream inputStream = new ObjectInputStream(byteArrayInputStream);

            result = (Map<String, Object>) inputStream.readObject();
            inputStream.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public byte[] testMap2Bytes(Map<String, Object> map) {
        byte[] bytes = null;
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(byteArrayOutputStream);

            outputStream.writeObject(map);
            outputStream.close();

            bytes = byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }
}
